﻿/*
 * RETRO 2012 - v2.3
 * 
 * PaRADIIT Project
 * https://sites.google.com/site/paradiitproject/
 * 
 * This software is provided under LGPL v.3 license, 
 * which exact definition can be found at the following link:
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Please, contact us for any offers, remarks, ideas, etc.
 * 
 * Copyright © RFAI, LI Tours, 2011-2012
 * Contacts : rayar@univ-tours.fr
 *            ramel@univ-tours.fr
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;     // NOT WPF, BUT NEEDED FOR FolderBrowserDialog

using Clustering;
using Retro.ViewModel;
using System.IO;  

namespace RetroGUI.clustering
{
    /// <summary>
    /// Define Clustering Panel
    /// </summary>
    public partial class ClusteringPanel : System.Windows.Controls.UserControl
    {

        //Attributes
        private TemplateMatchingParameters parameters = new TemplateMatchingParameters();
        private bool bIllustrationClustering;
        private String agoraProjectDir;
        private String outputClustersDir;


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="agoraProjectDir">Path of the AGORA project</param>
        /// <param name="outputClustersDir">Path of the output directory</param>
        public ClusteringPanel(String agoraProjectDir, String outputClustersDir)
        {
            InitializeComponent();

            this.agoraProjectDir = agoraProjectDir;
            this.outputClustersDir = outputClustersDir;
        }


        #region Events Handlers

        /// <summary>
        /// Handler for Select Existing Models Directory button
        /// Display a FolderBrowserDialog
        /// and update the associated textbox with the selected directory
        /// </summary>
        private void Click_Browse_Folder(object sender, RoutedEventArgs e)
        {
            // Open a dialog for forlder selection
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();

            if ((fbd.SelectedPath.Length != 0) && (Directory.Exists(fbd.SelectedPath)))
            {
                // Update textbox with the selected directory
                ExistingModelsDirectoryTextBox.Text = fbd.SelectedPath;
            }
        }


        /// <summary>
        /// Handler for threshold slider
        /// </summary>
        private void Threshold_Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.parameters.TEMPLATE_MATCHING_THRESHOLD = (float)(e.NewValue / 100.0);
        }


        /// <summary>
        /// Handler for binarization checkbox
        /// </summary>
        private void Binarize_CheckBox_Changed(object sender, RoutedEventArgs e)
        {
            this.parameters.BINARIZE_FOR_COMPARISON = (bool)this.binarizeCheckBox.IsChecked;
            // If binarize is uncheked, disable denoising
            if (!this.parameters.BINARIZE_FOR_COMPARISON)
            {
                this.denoiseCheckBox.IsChecked = false;
                this.parameters.DENOISE_BOUNDING_BOX = false;
                this.denoiseCheckBox.IsEnabled = false;
            }
            else
            {
                if (this.denoiseCheckBox != null)
                    this.denoiseCheckBox.IsEnabled = true;
            }
        }


        /// <summary>
        /// Handler for normalization checkbox
        /// </summary>
        private void Normalize_CheckBox_Changed(object sender, RoutedEventArgs e)
        {
            this.parameters.NORMALIZE_BEFORE_COMPARISON = (bool)this.normalizeCheckBox.IsChecked;
        }


        /// <summary>
        /// Handler for Denoising checkbox (binary images mandatory)
        /// </summary>
        private void Denoise_CheckBox_Changed(object sender, RoutedEventArgs e)
        {
            this.parameters.DENOISE_BOUNDING_BOX = (bool)this.denoiseCheckBox.IsChecked;
        }


        /// <summary>
        /// Handler for ILLUSTRATION checkbox
        /// </summary>
        private void Illustration_CheckBox_Changed(object sender, RoutedEventArgs e)
        {
            this.bIllustrationClustering = (bool)this.Illustration_CheckBox.IsChecked;

            // Propagate this information to the main window in a really dirty way
            // mandatory to load the correct path of the thumbnail regarding wheter it's a illustration or not
            ((RetroGUI.main.MainWindow)App.Current.MainWindow).bIllustrationClustering = this.bIllustrationClustering;
        }


        /// <summary>
        /// Handler for Start button
        /// </summary>
        private void Click_Start(object sender, RoutedEventArgs e)
        {
            if ((this.ExistingModelsDirectoryTextBox.Text.CompareTo("") != 0) && (!Directory.Exists(this.ExistingModelsDirectoryTextBox.Text)))
                System.Windows.MessageBox.Show("The existing models directory does not exists.", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
            else
                new ClusteringTool2(this.agoraProjectDir, this.ExistingModelsDirectoryTextBox.Text, this.outputClustersDir, this.parameters, this.bIllustrationClustering);
        }

        #endregion

    }
}
